<template>
  <div>
    <el-dialog
      :destroy-on-close="false"
      :show-close="false"
      v-model="wxDialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      align-center
      width="689"
    >
      <div class="login-box">
        <img class="close" @click="close" src="~/assets/img/close.png" alt="" />
        <div class="left">
          <el-carousel height="auto" autoplay>
            <el-carousel-item style="height: 414px">
              <img
                class="w-full h-full"
                src="~@/assets/img/login/swiper1.png"
                alt=""
              />
            </el-carousel-item>
            <el-carousel-item style="height: 414px">
              <img
                class="w-full h-full"
                src="~@/assets/img/login/swiper2.png"
                alt=""
              />
            </el-carousel-item>
            <el-carousel-item style="height: 414px">
              <img
                class="w-full h-full"
                src="~@/assets/img/login/swiper3.png"
                alt=""
              />
            </el-carousel-item>
          </el-carousel>
        </div>
        <div class="right items-center content-center">
          <div class="wx-login">
            <img class="code" :src="qr_url" alt="" />
            <div>{{ title }}</div>
            <!-- <div class="desc">
              <el-checkbox v-model="checked" size="large" />
              <span style="margin-left: 4px">扫码登录即表示您同意并遵守</span
              ><span>《用户协议》</span>
            </div> -->
          </div>
        </div>
      </div>
    </el-dialog>
    <el-dialog
      :destroy-on-close="false"
      :show-close="false"
      v-model="phoneDialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      align-center
      width="689"
    >
      <div class="login-box">
        <img class="close" @click="close" src="~/assets/img/close.png" alt="" />
        <div class="left">
          <el-carousel height="auto" autoplay>
            <el-carousel-item style="height: 414px">
              <img
                class="w-full h-full"
                src="~@/assets/img/login/swiper1.png"
                alt=""
              />
            </el-carousel-item>
            <el-carousel-item style="height: 414px">
              <img
                class="w-full h-full"
                src="~@/assets/img/login/swiper2.png"
                alt=""
              />
            </el-carousel-item>
            <el-carousel-item style="height: 414px">
              <img
                class="w-full h-full"
                src="~@/assets/img/login/swiper3.png"
                alt=""
              />
            </el-carousel-item>
          </el-carousel>
        </div>
        <div class="right items-center content-center">
          <div class="phone-login">
            <div class="title">绑定手机号</div>
            <div class="sub-title">提升账户安全。快捷登录更便捷</div>
            <el-form :model="form" class="form">
              <el-form-item>
                <el-input placeholder="请输入手机号" v-model="form.phone" />
              </el-form-item>
              <el-form-item>
                <el-input placeholder="请输入验证码" :readonly="!hasPhone" v-model="form.code">
                  <template #suffix> 
                    <el-button @click="getQrCode" :disabled="!hasPhone" link type="text">获取验证码</el-button>
                   </template>
                </el-input>
              </el-form-item>
              <el-form-item>
                <el-button :disabled="isDisabled" style="width: 100%" type="primary">登录</el-button>
              </el-form-item>
              <el-form-item>
                <el-button style="width: 100%" @click="closePhone">稍后绑定</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="LoginPop">
import { checkWxQrCode, getWxQrCode } from "~/apis/user";
const token = useToken();
const tokenCookie = useCookie("token");
const userInfoCookie = useCookie("userInfo");

const userInfo = useUserInfo();
const qr_url = ref("");
const tem_poll = ref(null);
const title = ref("扫描二维码登录");
const wxDialogVisible = useWxDialogVisible();
const phoneDialogVisible = usePhoneDialogVisible();

const hasPhone = computed(()=>{
  return form.value.phone
})

const closePhone = ()=>{
  phoneDialogVisible.value = false
}

const form = ref({
  phone: "",
  code: "",
});

const isDisabled = computed(()=>{
  return form.value.phone && form.value.code
})

// 获取验证码
const getQrCode = ()=>{

}

// 获取二维码
function getQrUrl() {
  getWxQrCode({})
    .then(({ data: res }) => {
      if (res.value.code === 0) {
        qr_url.value = res.value.data.url;
        tem_poll.value = setInterval(() => {
          loginPoll(res.value.data.state);
        }, 1000);
      }
    })
    .catch((err) => {
      console.log(err);
    });
}

// 轮询状态的接口
function loginPoll(state) {
  checkWxQrCode({
    query: { state },
  })
    .then(({ data: res }) => {
      if (res.value.code === 0) {
        if (res.value.data.status && res.value.data.status === 0) {
          title.value = "等待扫码";
        } else if (res.value.data.token) {
          clearInterval(tem_poll.value);
          wxDialogVisible.value = false;
          title.value = "扫码成功";
          tokenCookie.value = res.value.data.token;
          token.value = "Bearer " + res.value.data.token;
          userInfoCookie.value = res.value.data.user;
          userInfo.value = res.value.data.user;
          phoneDialogVisible.value = true;
        }
      } else {
        // 7 或者 10 让他重新刷新二维码
        title.value = res.value.msg;
        clearInterval(tem_poll.value);
        this.getQrUrl();
      }
    })
    .catch((err) => {
      console.log(err);
    });
}

onMounted(() => {
  getQrUrl();
});

const close = () => {
  wxDialogVisible.value = false;
  phoneDialogVisible.value = false;
};

onUnmounted(() => {
  tem_poll.value && clearInterval(tem_poll.value);
});
</script>

<style lang="scss" scoped>
.login-box {
  height: 414px;
  background: #ffffff;
  border-radius: 10px;
  display: flex;
  position: relative;
  .close {
    position: absolute;
    width: 20px;
    height: 20px;
    top: 10px;
    right: 10px;
    cursor: pointer;
  }
  .left {
    width: 310px;
    height: 100%;
  }
  .right {
    width: 379px;
    height: 100%;
    .wx-login {
      display: flex;
      flex-direction: column;
      align-items: center;
      .code {
        width: 270px;
        height: 270px;
      }
      .desc {
        margin-top: 16px;
        display: flex;
        align-items: center;
        span:first-child {
          font-size: 14px;
          font-weight: 400;
          text-align: LEFT;
          color: #333333;
        }
        span:last-child {
          font-size: 14px;
          font-weight: 400;
          text-align: LEFT;
          color: #ff5500;
          cursor: pointer;
        }
      }
    }
    .phone-login {
      display: flex;
      flex-direction: column;
      align-items: center;
      .title {
        font-size: 20px;
        font-weight: 400;
        color: #333333;
        margin-bottom: 10px;
        margin-top: 30px;
      }
      .sub-title {
        font-size: 14px;
        font-weight: 400;
        color: #333333;
        margin-bottom: 30px;
      }
      .form {
        width: 270px;
        height: 270px;
      }
    }
  }
}
</style>
